package com.ljkj.cordial.chat.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.BaseActivity;
import com.ljkj.cordial.chat.util.ObserverThread;
import com.ljkj.cordial.chat.widget.VideoPlayer;
import com.nic.view.util.glide.GlideUtil;
import com.yanzhenjie.album.util.AlbumUtils;

public class VideoPlayActivity extends BaseActivity {


    private String path;
    private String thumb;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_video_play;
    }

    @Override
    public void initView() {
        path = getIntent().getStringExtra("path");
        thumb = getIntent().getStringExtra("thumb");
        VideoPlayer mVideoPlayer = (VideoPlayer) findViewById(R.id.vPlayer);
        if (!TextUtils.isEmpty(thumb)) {
//            BitmapDrawable bd = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(thumb));
//            mVideoPlayer.setBackground(bd);
            mVideoPlayer.getIvThumb().setImageBitmap(BitmapFactory.decodeFile(thumb));
        } else {
            ObserverThread.create().onNextThread0(new ObserverThread.OnActionListener<Bitmap>() {
                @Override
                public void onAction(Bitmap bmp) {
                    if (bmp != null) {
                        GlideUtil.getInstance().loadSmall(bmp, mVideoPlayer.getIvThumb());
                    }
                }

                @Override
                public Bitmap onThreadAction() {
                    return AlbumUtils.takePicture(path);
                }
            });
        }
        mVideoPlayer.postDelayed(new Runnable() {
            @Override
            public void run() {
                mVideoPlayer.setUrl(path);
                mVideoPlayer.enableAuto();
//                mVideoPlayer.setResizeMode(VideoPlayer.RESIZE_MODE_CONTAIN);
            }
        }, 300);

    }

    public static void jumpTo(Context context, String path, String thumb) {
        Intent intent = new Intent(context, VideoPlayActivity.class);
        intent.putExtra("path", path);
        intent.putExtra("thumb", thumb);
        context.startActivity(intent);
    }
}
